/*
 * Speaker.java
 * This is the client-side application. It uses Connector to setup a socket with the server-side application
 * So the connection is transparent. you don't have to know the cluster structure.
 * The speaker just send some message, and then print to the console the message replied by the server-side
 * application  
 * 
 * */

package wps.servicebus.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

class Speaker{
	Socket speaker = null;
	PrintWriter out = null;	
	BufferedReader in = null;
	
	public Speaker() throws ClientProtocolException, IOException, ParseException{
		//setup the connection with the backend application server without knowledge
		//of its existence
		Connector con = new Connector();
		//pass the established socket to the client application and it is DONE!!!
		speaker = con.getSocket();
		//the client application is written as usual
		out = new PrintWriter(speaker.getOutputStream(), true);			
		in = new BufferedReader(new InputStreamReader(speaker.getInputStream()));
	}

	public void speak(String message){
		out.println(message);		
	}
	public void listen(){
		String input;
		try {
			while((input = in.readLine()) != null){
				System.out.println(input);			
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("Speaker quitting...");
		}
	}
	public void close() throws IOException{
		speaker.close();
		out.close();		
		in.close();
	}
	
	public static void main(String[] args) throws IOException, ParseException{
		
		Speaker s = new Speaker();;
		s.speak("I am in zhuhai");
		s.listen();
		s.close();
	}	
}
